//
//  AppDelegate.swift
//  WeiBo
//
//  Created by Apple on 16/9/4.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        //  监听切换根视图控制器的通知, 这个地方可以加参数，也可以不加，不加这个参数默认也会调用有参数的这个方法，把通知的参数传过去
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootVC(noti:)), name: NSNotification.Name(SwitchRootVCNotification), object: nil)
        
        //  1.创建window
        //  swift 3.0，改成了UIScreen.main
        window = UIWindow(frame: UIScreen.main.bounds)
        //  2.创建跟视图控制器
        let rootVC = CZMainViewController()
        //  3.设置跟视图控制器
        window?.rootViewController = rootVC
        //  4.设置为主窗口并显示
        window?.makeKeyAndVisible()
        
        return true
    }

    // MARK:    --切换跟视图控制器
    @objc private func switchRootVC(noti: Notification) {
        
        let object = noti.object
        //  判断传入的过来的对象是否是授权登录页面，是进入欢迎页面，否则进入主页
        if object is CZOAuthViewController {
            window?.rootViewController = CZWelcomeViewController()
        } else {
            window?.rootViewController = CZMainViewController()
        }
        
        
    }
    
    
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        CZStatusDAL.clearCacheData()
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

